package cn.wolfcode.cms.service.impl;

import cn.wolfcode.cms.domain.Membernum;
import cn.wolfcode.cms.domain.NumCharge;
import cn.wolfcode.cms.domain.Product;
import cn.wolfcode.cms.mapper.MembernumMapper;
import cn.wolfcode.cms.mapper.NumChargeMapper;
import cn.wolfcode.cms.mapper.ProductMapper;
import cn.wolfcode.cms.query.QueryObject;
import cn.wolfcode.cms.service.INumChargeService;
import cn.wolfcode.cms.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class NumChargeServiceImpl implements INumChargeService {

    @Autowired
    private NumChargeMapper numChargeMapper;
    @Autowired
    private MembernumMapper membernumMapper;
    @Autowired
    private ProductMapper productMapper;

    public void save(NumCharge entity) {
        numChargeMapper.insert(entity);
    }

    public void save(Map<String, String> entity) {
        try {
            String ProducrName = entity.get("commodity.name");//service.name
            String cardNum = entity.get("client_number");//卡
            String addnum = entity.get("times");//addendu
            String overtime = entity.get("outTime");//有效时间
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            String pay_id = entity.get("extype");//type_id
            Date parse = simpleDateFormat.parse(overtime);
            Long id = productMapper.getIdByName(ProducrName);
            Membernum membernum = membernumMapper.selectMemberNumByCardNumAndProId(cardNum, id);
            NumCharge numCharge = new NumCharge();
            Product product = new Product();
            if (membernum == null) {
                product.setId(id);
                membernum = new Membernum();
                membernum.setTotalnum(Integer.valueOf(addnum));
                membernum.setSubnum(Integer.valueOf(addnum));
                membernum.setCard_num(Integer.valueOf(cardNum));
                membernum.setService(product);
                membernum.setOvertime(parse);
                membernumMapper.insert(membernum);
                numCharge.setOldnum(0);
            } else {
                membernum.setTotalnum(membernum.getTotalnum() + Integer.valueOf(addnum));
                membernum.setSubnum(membernum.getSubnum() + Integer.valueOf(addnum));
                membernum.setOvertime(parse);
                membernumMapper.updateByPrimaryKey(membernum);
                numCharge.setOldnum(membernum.getTotalnum());
            }
            entity.get("commodity.sn");//sn
            numCharge.setAddnum(Integer.valueOf(addnum));
            numCharge.setCard_num(Integer.valueOf(cardNum));
            numCharge.setPay_id(Long.valueOf(pay_id));
            numCharge.setNewnum(numCharge.getAddnum() + numCharge.getOldnum());
            numCharge.setPaytime(new Date());
            numCharge.setService(product);
            numCharge.setOpnum(Long.valueOf(pay_id));
            numChargeMapper.insert(numCharge);
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    public void update(NumCharge entity) {
        numChargeMapper.updateByPrimaryKey(entity);
    }

    public void delete(Long id) {
        numChargeMapper.deleteByPrimaryKey(id);
    }

    public NumCharge get(Long id) {
        return numChargeMapper.selectByPrimaryKey(id);
    }

    public List<NumCharge> list() {
        return numChargeMapper.selectAll();
    }

    public Object query(QueryObject qo) {
        //查总数
        int total = numChargeMapper.queryForCount(qo);
        if (total == 0) {
            return new PageResult();
        }
        //查数据
        List<NumCharge> rows = numChargeMapper.queryForList(qo);
        return new PageResult(total, rows);
    }

    public  List<NumCharge> getRecordBykeyInfo(String keyInfo) {
        return numChargeMapper.getRecordBykeyInfo(keyInfo);
    }

}
